<?php

class Auth extends MY_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->load->view('backend/auth_index_view');
    }

    public function login() {
        $password = $this->input->post('password');

        $username = trim($this->input->post('username'));

        $this->load->model('user_model');

        $user = $this->user_model->find(array(array('username', $username)));

        if ($user && ($user->password == md5($password))) {
            $this->session->set_userdata(array(
                'g_user_id' => $user->id,
                'g_user_name' => $user->username
            ));
            $this->json_back('XT001', '登录成功');
        } else {
            $this->json_back('XT002', '您的帐号或密码错误');
        }
    }

    public function logout() {
        $this->session->unset_userdata('g_user_id');

        $this->session->sess_destroy();

		$login_url = SITE_URL.'/index.php/backend/auth/index';
		header("Location: {$login_url}");
		exie(0);
    }

}